

# to manipulate data
library(tidyverse)

# to generate missingness map
library(visdat)


# load data
load("full_data_conditionality_covariates.RData")


# descriptive statistics
descriptive_statistics <- full_df %>%
  select(year,short_term,long_term,
         imf_program,resource_conditionality,resource_conditionality_dict_tfidf,
         resource_conditionality_dummy,resource_conditionality_count_abs,resource_conditionality_count_rel,
         log_gdp_pc_constant,gdp_growth,resource_rents,log_external_debt_stocks,working_age_pop,
         field_discovery,oil_price,crisis,wb_extractive_project,
         polity2,left_executive,election,
         nationalization_oc,any_war,eiti_member) %>%
  mutate(resource_conditionality = resource_conditionality * 100,
         resource_conditionality_count_rel = resource_conditionality_count_rel * 100)

# TABLE
descriptive_statistics <- as.data.frame(descriptive_statistics)
stargazer(descriptive_statistics, title="Descriptive Statistics", #type="text", 
          covariate.labels = c("Year","Short-Term Policy","Long-Term Policy",
                               "Program Participation",
                               "Resource Conditionality (%)",
                               "Resource Conditionality (TF--IDF)",
                               "Natural Resource Condition",
                               "Natural Resource Conditions (Count)",
                               "Natural Resource Condition (% of All Conditions)",
                               "GDP per Capita (USD, Log)",
                               "GDP Growth (%)",
                               "Resource Rents (% GDP)",
                               "External Debt Stocks (% GNI, log)",
                               "Working Age Population (%)",
                               "Field Discovery",
                               "Oil Price (USD)",
                               "Crisis",
                               "WB Extractive Project",
                               "Democracy (Polity2)",
                               "Left Executive",
                               "Election Year",
                               "Oil Company Nationalization",
                               "War",
                               "EITI Member"), 
          digit.separate=0,
          omit.summary.stat=c("p25","p75"))


# PLOT
descriptive_statistics %>%
  rename(`Year` = year,
         `Short-Term Policy` = short_term,
         `Long-Term Policy` = long_term,
         `Program Participation` = imf_program,
         `Resource Conditionality (%)` = resource_conditionality,
         `Resource Conditionality (TF--IDF)` = resource_conditionality_dict_tfidf,
         `Natural Resource Condition` = resource_conditionality_dummy,
         `Natural Resource Conditions (Count)` = resource_conditionality_count_abs,
         `Natural Resource Condition (% of All Conditions)` = resource_conditionality_count_rel,
         `GDP per Capita (USD, Log)` = log_gdp_pc_constant,
         `GDP Growth (%)` = gdp_growth,
         `Resource Rents (% GDP)` = resource_rents,
         `External Debt Stocks (% GNI, log)` = log_external_debt_stocks,
         `Working Age Population (%)` = working_age_pop,
         `Field Discovery` = field_discovery,
         `Oil Price (USD)` = oil_price,
         `Crisis` = crisis,
         `WB Extractive Project` = wb_extractive_project,
         `Democracy (Polity2)` = polity2,
         `Left Executive` = left_executive,
         `Election Year` = election,
         `Oil Company Nationalization` = nationalization_oc,
         `War` = any_war,
         `EITI Member` = eiti_member) %>%
  vis_miss(sort_miss = T, show_perc_col = F) +
  theme(plot.margin = unit(c(0,3.5,0,0.5), "cm"))

ggsave("figure5.pdf", width = 10, height = 7.5)
